//
//  MainTabBar.swift
//  weibo
//
//  Created by Mike on 15/5/10.
//  Copyright (c) 2015年 Mike. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {
    
    lazy var composeBtn: UIButton = {/*()-> UIButton in*/
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button")!, forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add")!, forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted")!, forState: UIControlState.Highlighted)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted")!, forState: UIControlState.Highlighted)
        // 添加到tabbar
        self.addSubview(btn)
        btn.addTarget(self, action: "click", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        }()
    @objc private func click() {
        println("haha")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
//        print(self.subviews as Array)
//        UITabBarButton
        let btnCount = 5
        let w = self.bounds.size.width / CGFloat(btnCount)
        let h = self.bounds.size.height
        var initFrame = CGRectMake(0, 0, w, h)
        
        var index = 0
        // 重新排列四个按钮
        for view in self.subviews as! [UIView] {
//            println(view)
            if view is UIControl && !(view is UIButton) {
//                println(view)
                view.frame = CGRectOffset(initFrame, w * CGFloat(index), 0)
                
//                if index == 1 {
//                    index += 2
//                } else {
//                    index++
//                }
                index += index == 1 ? 2 : 1
                
            }
        }
        // 设置发布消息的按钮
//        self.composeBtn.frame = CGRectOffset(initFrame, w * 2.0, 0)// 这样设置的frame不一定是在中心的 当按钮较小的时候就会偏上
        self.composeBtn.frame = initFrame // 这句是为了使用initFrame的宽度和高度 后面还需要修正x和y
        self.composeBtn.center = CGPointMake(self.center.x, h * 0.5)
    }
}
